<?php
class App_Module_Admin_Component_Groups_Ctrl_Http_Web extends Mozg_Controller_Instance
{
	private $_moduleId;
	private $_ctrlId;
	private $_id;

    protected function PrimaryActionInit ($basicActionName)
    {
        Mozg::Layout('admin')->SetMenuActiveElement('access/groups');
    }

    /**
		экшен по-умолчанию
	*/
    public function PrAction ()
    {
        $this->listPr();
    }
    
    private function GetQueryId()
    {
    	$this->_id = intval(Mozg::Request('http')->get('id'));
        if ($this->_id)
        {
			$group = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $this->_id));
			if (!$group)
			{
				$this->_id = 0;
			}
        }
        return $this->_id;
    }
    
    private function GetQueryModuleId()
    {
    	$this->_moduleId = Mozg::Request('http')->get('ctrlid');
        if (Mozg_Module::GetNameById($this->_moduleId) === false)
        {
            $this->_moduleId = Mozg_Module::GetId($this->GetModuleName());
        }
        
        return $this->_moduleId;
    }
    
    private function GetQueryCtrlId()
    {
    	if (!$this->GetQueryModuleId())
    	{
			$this->_ctrlId = 0;
			return $this->_ctrlId;
    	}
    	
    	$this->_ctrlId = intval(Mozg::Request('http')->get('modid'));
        if (Mozg_Module::GetCtrlNameById($this->_moduleId, $this->_ctrlId) === false)
        {
            $this->_ctrlId = 0;
        }
        
        return $this->_ctrlId;
    }
    
    public function savePrAction ()
    {
   		if (!$this->GetQueryId()) $group = Mozg_Mapper::Get('Groups')->GetEntity();
    	else $group = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $this->_id));

   		$this->GetTpl('Editform')->AddPostData($group, (array)Mozg::Request('http')->getPost('group'));
        
        if (!Mozg_Mapper::Get('Groups')->Save($group))
        {
        	if (!$group->Validator()->GetState())
        	{
            	Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('INVALID_DATA'), MESSAGE_ERROR);
	       		$this->addPr($group);
	       		return;
        	}
        }
        else
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('GROUP_SAVED'));
        }

       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
    }
    
    public function addPrAction ($group = null)
    {
        if (!$group)
        {
	   		if (!$this->GetQueryId() || !in_array($this->_id, Mozg::User()->GetAvailableGroups())) $group = Mozg_Mapper::Get('Groups')->GetEntity();
	   		else $group = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $this->_id));
        }

        $t = $this->GetTpl('Editform');
        Mozg::Response('http')->SetBody($t->GetForm($group));
    }
    
    public function listPrAction ()
    {
        $t = $this->GetTpl('List');
        Mozg::Response('http')->SetBody($t->GetList());
    }
    
    public function delPrAction ()
    {
    	if (!$this->GetQueryId() || !in_array($this->_id, Mozg::User()->GetAvailableGroups()))
    	{
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('GROUP_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
    	}
    	
        if (!Mozg_Mapper::Get('Groups')->Delete(array('id' => $this->_id)))
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('UNABLE_TO_DELETE_GROUP'), MESSAGE_ERROR);
        }
        else
        {
			Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('GROUP_DELETED'));
        }
        
       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
    }
    
    public function viewPrAction ()
    {
        if (!$this->GetQueryId() || !in_array($this->_id, Mozg::User()->GetAvailableGroups()))
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('GROUP_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
        }

        if (!$this->GetQueryModuleId())
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('MODULE_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
        }
        
        $group = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $this->_id));
        $parentGroup = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $group->GetParentId()));
        
        if (!$parentGroup)
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('GROUP_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
        }
        
        $avCtrls = Mozg_Module::GetCtrlList(Mozg_Module::GetNameById($this->_moduleId));
        
        if (!Mozg_Mapper::Get('Groups')->IsSuperUserGroup($parentGroup->GetId()))
        {
	        $moduleAccess = Mozg_Mapper::Get('Groups')->GetGroupsAccess($parentGroup->GetId(), $this->_moduleId);
	        if (isset($moduleAccess[$parentGroup->GetId()][$this->_moduleId]))
	        {
	        	$ctrls = array();
	        	foreach ($avCtrls as $avKey => $av)
	        	{
	        		if (Mozg_Mapper::Get('Groups')->CheckAccess($moduleAccess[$parentGroup->GetId()][$this->_moduleId], $av['id']))
	        		{
	        			$ctrls[$avKey] = $av;
	        		}
	        	}
	        	
	        	$avCtrls = $ctrls;
	        }
	        else
	        {
	        	$avCtrls = array();
	        }
        }
                
        $t = $this->GetTpl('Web');
        $t->moduleId = $this->_moduleId;
        $t->group = $group;
        $t->ctrls = $avCtrls;
        Mozg::Response('http')->SetBody($t->ViewGroup());
        Mozg::SubAction()->Set('Left Sidebar', array($this, 'out_modules'));
    }
    
    public function out_modules()
    {
        $t = $this->GetTpl('Web');
        $t->moduleId = $this->_moduleId;
        $t->groupId = $this->_id;
        $t->data = array();
        $t->group = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $this->_id));
        
        $modules = Mozg_Module::GetList();
        foreach ($modules as $mod)
        {
        	$t->data[$mod['id']] = $mod;
        }
 
        return $t->OutModules();
    }
    
    public function modulePrAction ()
    {
        if (!$this->GetQueryId() || !in_array($this->_id, Mozg::User()->GetAvailableGroups()))
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('GROUP_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
        }

        if (!$this->GetQueryCtrlId())
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('CONTROLLER_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
        }
        
        $group = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $this->_id));
        $parentGroup = Mozg_Mapper::Get('Groups')->GetOne(array('id' => $group->GetParentId()));
        
        if (!$parentGroup)
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('GROUP_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
        }

        $ctrlsActions = Mozg_Module::GetCtrlActionList(Mozg_Module::GetNameById($this->_moduleId), Mozg_Module::GetCtrlNameById($this->_moduleId, $this->_ctrlId));
        $moduleAccess = Mozg_Mapper::Get('Groups')->GetGroupsAccess(array($group->GetId(), $parentGroup->GetId()), $this->_moduleId);
        
        if (!Mozg_Mapper::Get('Groups')->IsSuperUserGroup($parentGroup->GetId()))
        {
	        if (isset($moduleAccess[$parentGroup->GetId()][$this->_moduleId]))
	        {
	        	$parentModuleAccess = $moduleAccess[$parentGroup->GetId()][$this->_moduleId];
	        	$ctrlsIds = array();
	        	while (list($k, $v) = each($ctrlsActions))
	        	{
				    if (!Mozg_Mapper::Get('Groups')->CheckAccess($parentModuleAccess, $this->_ctrlId, $v['id'])) continue;
				    $ctrlsIds[$k] = $v;
	        	}
        	
	        	$ctrlsActions = $ctrlsIds;
	        }
	        else
	        {
	        	$ctrlsActions = array();
	        }
        }
        
        if (isset($moduleAccess[$group->GetId()][$this->_moduleId]))
        {
        	$curModuleAccess = $moduleAccess[$group->GetId()][$this->_moduleId];
       	    while (list($k, $v) = each($ctrlsActions))
        	{
			    if (!Mozg_Mapper::Get('Groups')->CheckAccess($curModuleAccess, $this->_ctrlId, $v['id'])) continue;
			    $ctrlsActions[$k]['enabled'] = true;
        	}
        	reset($ctrlsActions);
        }
        
        $t = $this->GetTpl('Web');
        $t->moduleId = $this->_moduleId;
        $t->ctrlId = $this->_ctrlId;
        $t->group = $group;
        
        $t->ctrl = Mozg_Module::GetCtrlDetails(Mozg_Module::GetNameById($this->_moduleId), Mozg_Module::GetCtrlNameById($this->_moduleId, $this->_ctrlId));
        $t->ctrlActions = $ctrlsActions;

        Mozg::Response('http')->SetBody($t->ViewModule());
        Mozg::SubAction()->Set('Left Sidebar', array($this, 'out_modules'));
    }
    
    public function savemodulePrAction ()
    {
        if (!$this->GetQueryId())
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('GROUP_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
        }
    	
        if (!$this->GetQueryCtrlId())
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('CONTROLLER_NOT_FOUND'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect('module=admin&ctrl=groups');
        }
        
        $actions = (array)Mozg::Request('http')->getPost('act');
        $result = Mozg_Mapper::Get('Groups')->SaveGroupAccess($this->_id, $this->_moduleId, $this->_ctrlId, $actions);
        if (!$result)
        {
            Mozg::Lib('messanger')->createMessage(Mozg_Lang::_('UNABLE_TO_SAVE_ACCESS'), MESSAGE_ERROR);
	       	Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'groups', 'action' => 'view', 'id' => $this->_id , 'ctrlid' => $this->_moduleId));
        }
        
        Mozg::Lib('messanger')->createMessage('Доступ сохранён');
        Mozg_Router::Get('http')->Redirect(array('module' => 'admin', 'ctrl' => 'groups', 'action' => 'view', 'id' => $this->_id , 'ctrlid' => $this->_moduleId));
    }
}